import { login } from '@/api'
import { defineStore } from 'pinia'
import { ref } from 'vue'
export const useUserStore = defineStore('user', () => {
  // state
  const userInfo = ref(null)
  const token = ref(null)
  const menus = ref([])
  // actions
  const loginAction = async (data, callback) => {
    
    try {
      const res = await login(data)
      if (res) {
        token.value = res.token
        userInfo.value = res.user
        menus.value = res.menus
        callback(true)
      } else {
        callback(false)
      }  
    } catch {
      callback(false)
    }
  }
  const logoutAction = () => {
    userInfo.value = null
    token.value = null
    menus.value = []
  }

  return {
    userInfo,
    token,
    menus,
    loginAction,
    logoutAction
  }
})